package com.ly.web.controller.user;

import com.ly.base.ApiResponse;
import com.ly.base.HouseSubscribeStatus;
import com.ly.base.LoginUserUtil;
import com.ly.service.IUserService;
import com.ly.service.ServiceMultiResult;
import com.ly.service.ServiceResult;
import com.ly.service.house.IHouseService;
import com.ly.web.dto.HouseDTO;
import com.ly.web.dto.HouseSubscribeDTO;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.util.Pair;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 用户接口
 * @author ：LY
 * @date ：Created in 2020/11/9 18:54
 * @modified By：
 */
@Controller
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IHouseService houseService;
    @GetMapping("/user/login")
    public String  loginPage(){
        return "user/login";
    }
    @GetMapping("/user/center")
    public String centerPage(){
    return "user/center";
    }


    /**
     * 更新用户信息
     * @param profile
     * @param value
     * @return
     */
    @PostMapping("api/user/info")
    @ResponseBody
    public ApiResponse updateUserInfo(@RequestParam(value = "profile")String profile,
                                      @RequestParam(value = "value")String value){
        if (value.isEmpty()){
            return ApiResponse.ofStatus(ApiResponse.Status.BAD_REQUEST);
        }
        if ("email".equals(profile) && LoginUserUtil.checkEmail(value)) {

            return ApiResponse.ofMessage(HttpStatus.SC_BAD_REQUEST,"不支持的邮箱格式");

        }
        ServiceResult result = userService.modifyUserProfile(profile, value);
       if (result.isSuccess()){
           return ApiResponse.ofSuccess("");
       }else {
           return ApiResponse.ofMessage(HttpStatus.SC_BAD_REQUEST,result.getMessage());
       }
    }

    /**
     * 更新预约状态api
     * @param houseId
     * @return
     */
    @PostMapping(value = "api/user/house/subscribe")
    @ResponseBody
    public ApiResponse subscribeHouse(@RequestParam(value = "house_id")Long houseId){
        ServiceResult result = houseService.addSubscribeOrder(houseId);
        if (result.isSuccess()){
            return ApiResponse.ofSuccess("");
        }else {
            return ApiResponse.ofMessage(HttpStatus.SC_BAD_REQUEST,result.getMessage());
        }
    }

    /**
     * 获取对应状态的预约数据
     * @param start
     * @param size
     * @param staus
     * @return
     */
    @GetMapping("api/user/house/subscribe/list")
    @ResponseBody
    public ApiResponse subscribeList(@RequestParam(value = "start",defaultValue = "0")int start,
                                     @RequestParam(value = "size",defaultValue = "3") int size,
                                     @RequestParam(value = "status")int staus){

        ServiceMultiResult<Pair<HouseDTO, HouseSubscribeDTO>> result = houseService.querySubscribeList(HouseSubscribeStatus.of(staus), start, size);

        if (result.getResultSize() == 0){
            return ApiResponse.ofSuccess(result.getResult());
        }
        ApiResponse response = ApiResponse.ofSuccess(result.getResult());
        response.setMore(result.getTotal()>(start+size));
        return response;
    }


    /**
     * 用户预约看房接口
     * @param houseId 房源ID
     * @param orderTime 预约时间
     * @param desc  详细描述
     * @param telephone 预约电话
     * @return
     */
    @PostMapping(value = "api/user/house/subscribe/date")
    @ResponseBody
    public ApiResponse subscribeDate(
            @RequestParam(value = "houseId")Long houseId,
            @RequestParam(value = "orderTime") @DateTimeFormat(pattern = "yyyy-MM-dd")Date orderTime,
            @RequestParam(value = "desc",required = false)String desc,
            @RequestParam(value = "telephone") String telephone
            ){
        if (orderTime == null){
            return ApiResponse.ofMessage(HttpStatus.SC_BAD_REQUEST,"请选择预约时间");

        }
        if (!LoginUserUtil.checkTelephone(telephone)){
            return ApiResponse.ofMessage(HttpStatus.SC_BAD_REQUEST,"手机格式不正确");
        }
        ServiceResult result = houseService.subscribe(houseId, orderTime, telephone, desc);

        if (result.isSuccess()){
            return ApiResponse.ofStatus(ApiResponse.Status.SUCCESS);
        }else {
            return ApiResponse.ofMessage(HttpStatus.SC_BAD_REQUEST,result.getMessage());
        }
    }

    /**
     * 取消预约看房
     * @param houseId
     * @return
     */
    @DeleteMapping(value = "api/user/house/subscribe")
    @ResponseBody
    public  ApiResponse  cancelSubscribe(@RequestParam(value = "houseId")Long houseId){
        ServiceResult result = houseService.cancelSubcribe(houseId);
        if (result.isSuccess()){
            return ApiResponse.ofStatus(ApiResponse.Status.SUCCESS);
        }else {
            return ApiResponse.ofMessage(HttpStatus.SC_BAD_REQUEST,result.getMessage());
        }
    }


}
